package GUI.UI;
//登录实现在82行
//注册在91行(后端不用管)
//边框不可见在28行(后端不用管)
import GUI.component.BackGroundPanel;
import GUI.util.PathUtils;
import GUI.util.ScreenUtils;
import Operation.Login;
import Operation.jdbc;
import User.administrators;
import User.commonuser;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

public class GUI_LOGIN {
    //登陆界面
    JFrame jf=new JFrame("足球管理系统");

    final int WIDTH=1280;
    final int HEIGHT=720;

    //组装视图
    public void init() throws Exception{
        //设置窗口相关属性
//        jf.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);//设置坐标和大小
        jf.setBounds(0,0,ScreenUtils.getScreenWidth(),ScreenUtils.getScreenHeight());//设置坐标和大小
        jf.setUndecorated(true);//设置边框不可见
        jf.setResizable(false);//大小不可变
        //设置窗口内容
        //设置背景图片

        BackGroundPanel bgPanel=new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("LOGIN.png"))));

        //组装登陆相关元素
        Box vBox=Box.createVerticalBox();

        //组装用户名
        Box uBox=Box.createHorizontalBox();
        JLabel uLabel = new JLabel("用户名:");
        uLabel.setFont(new Font("黑体",Font.PLAIN,ScreenUtils.getScreenWidth()/60));//设置字体，风格，大小
        uLabel.setForeground(Color.WHITE);//设置文字颜色
        JTextField uField=new JTextField(ScreenUtils.getScreenWidth()/70);


        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(ScreenUtils.getScreenWidth()/100));
        uBox.add(uField);

        //组装密码
        Box pBox=Box.createHorizontalBox();
        JLabel pLabel = new JLabel("密  码:");
        pLabel.setFont(new Font("黑体",Font.PLAIN,ScreenUtils.getScreenWidth()/60));
        pLabel.setForeground(Color.WHITE);
        JTextField pField=new JTextField(ScreenUtils.getScreenWidth()/70);


        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(ScreenUtils.getScreenWidth()/100));
        pBox.add(pField);

        //组装按钮
        Box btnBox=Box.createHorizontalBox();
        JButton loginBtn=new JButton("登录");
//        loginBtn.setPreferredSize(new Dimension(ScreenUtils.getScreenWidth()/20, ScreenUtils.getScreenHeight()/10));
        loginBtn.setFont(new Font("黑体",Font.PLAIN,ScreenUtils.getScreenWidth()/60));
        JButton registBtn=new JButton("注册");
//        registBtn.setPreferredSize(new Dimension(ScreenUtils.getScreenWidth()/20, ScreenUtils.getScreenHeight()/10));
        registBtn.setFont(new Font("黑体",Font.PLAIN,ScreenUtils.getScreenWidth()/60));

        JButton exitBtn=new JButton("退出");
        exitBtn.setFont(new Font("黑体",Font.PLAIN,ScreenUtils.getScreenWidth()/60));

        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户输入的数据
                String username = uField.getText().trim();
                String passward = pField.getText().trim();
                //访问登录接口
                ArrayList<administrators> administrators_array = new ArrayList<>();
                ArrayList<commonuser> users_array = new ArrayList<>();
                jdbc bc=new jdbc("jdbc:mysql://rm-2ze4l9h346r9853u8jo.mysql.rds.aliyuncs.com :3306/fmms","root","@FMMS201");
                administrators_array=bc.search_adminstrators_Message();
                users_array=bc.search_users_Message();
                Login l=new Login();
                l.login(username,passward,administrators_array,users_array);
                ////////////////在此实现登录/////////////////////////

            }
        });

        registBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //跳转到注册页面
                ////////////////在此实现注册///////////////////////////

            }
        });

        exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(ScreenUtils.getScreenWidth()/20));
        btnBox.add(registBtn);
        btnBox.add(Box.createHorizontalStrut(ScreenUtils.getScreenWidth()/20));
        btnBox.add(exitBtn);

        vBox.add(Box.createVerticalStrut((int)(ScreenUtils.getScreenHeight()/2.3)));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(ScreenUtils.getScreenHeight()/20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(ScreenUtils.getScreenHeight()/10));
        vBox.add(btnBox);

        bgPanel.add(Box.createHorizontalStrut((int)(ScreenUtils.getScreenWidth()/2.3)));
        bgPanel.add(vBox);
        jf.add(bgPanel);

        jf.setDefaultCloseOperation(3);
        jf.setVisible(true);
    }

    //客户端程序入口
    public static void main(String[] args){
        try {
            new GUI_LOGIN().init();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
